#!/bin/sh
#
mos=/usr/local/etc/mos
#
# Get GET request parameters
# $1 - name of parameter
#
getParam () {
  echo $QUERY_STRING | sed -n "s/^.*$1=\([^&]*\).*$/\1/p"
}
#
# Get variable value from file
# $1 - name of file
# $2 - name of parameter
#
getValueFromFile() {
  cr=$( printf "\r" )
  cat $1 | sed -n "s/$cr//;s/^ *$2 *= *\(.*\)$/\1/p"
}
#
# Get element from "	" quoted list
# $1 - list
# $2 - element position
#
getElement() {
  echo "$1" | cut -d"	" -f$2
}
#
#===============================================
#
# Load language strings
#
eval $( cat $mos/mounts/lang/$( getValueFromFile $mos/core/mos.conf Lang ) | sed '/^$/d' )
#
#===============================================
#
# POST data
#
cPoint="/tmp/ramfs/volumes/mymount"
cDevice=
cFS="nfs"
cOption=
cLabel="mymount"

if [ "$REQUEST_METHOD" = "POST" ] ; then

  cPoint=$( getValueFromFile /tmp/post point )
  cDevice=$( getValueFromFile /tmp/post dev )
  cFS=$( getValueFromFile /tmp/post fs )
  cOption=$( getValueFromFile /tmp/post option )
  cLabel=$( getValueFromFile /tmp/post label )

  rm -f /tmp/post

  if [ ! -z "$cPoint$cDev$cFS" ] ; then
    if [ -f $mos/mounts/mounts.conf ] ; then
      if [ ! -z "$( cat $mos/mounts/mounts.conf | grep ^$cPoint )" ] ; then
        # edit
        ss="s/^"$( echo "$cPoint	" | sed 's/\//\\\//g' )".*$/"$( echo "$cPoint	$cDevice	$cFS	$cOption	$cLabel" | sed 's/\//\\\//g' )"/"
        sed -ie "$ss" $mos/mounts/mounts.conf
      else
        # add
        echo "$cPoint	$cDevice	$cFS	$cOption	$cLabel" >> $mos/mounts/mounts.conf
      fi
    else
      echo "$cPoint	$cDevice	$cFS	$cOption	$cLabel" > $mos/mounts/mounts.conf
    fi
  fi
fi
#
#===============================================
#
# Actions
#
act=$( getParam action )
if [ ! -z "$act" ] ; then

  po=$( getParam point )
  if [ ! -z "$po" ] ; then
    ss="/^"$( echo "$po	" | sed 's/\//\\\//g' )".*$/p"
    str=$( cat $mos/mounts/mounts.conf | sed -n  "$ss")
    if [ ! -z "$str" ] ; then
      cPoint=$( echo "$str" | cut -d"	" -f 1 )
      cDevice=$( echo "$str" | cut -d"	" -f 2 )
      cFS=$( echo "$str" | cut -d"	" -f 3 )
      cOption=$( echo "$str" | cut -d"	" -f 4 )
      cLabel=$( echo "$str" | cut -d"	" -f 5 )
    fi
# ----------------------------------------------
    if [ "$act" = "edit" ] ; then

      echo ""
# ----------------------------------------------
    elif [ "$act" = "delete" ] ; then

      ss="/^"$( echo "$po	" | sed 's/\//\\\//g' )".*$/d"
      sed -ie "$ss" $mos/mounts/mounts.conf

      [ -d "$po" ] && [ ! -z "$( ls $po )" ] && umount "$po"
      [ -d "$po" ] && rmdir "$po"

# ----------------------------------------------
    elif [ "$act" = "mount" ] ; then

      co=$cOption

      [ ! -z "$co" ] && co=" -o $co"

       [ ! -d "$cPoint" ] && mkdir -p "$cPoint"
       [ -d "$cPoint" ] && mount -t $cFS$co "${cDevice}" "${cPoint}"

# ----------------------------------------------
    elif [ "$act" = "umount" ] ; then

      [ -d "$po" ] && [ ! -z "$( ls $po )" ] && umount "$po"
      [ -d "$po" ] && rmdir "$po"

    fi
  fi
fi
#
#===============================================
#
# Begin html
#
echo "<h3>$tMounts</h3>
<hr />"

if [ -f $mos/mounts/mounts.conf ] ; then

echo "<table class=\"s_list\" valign=\"middle\" cellpadding=\"4\" cellspacing=\"0\" >
<tr class=\"c_dis\">
<th>$tPoint</th>
<th>$tDevice</th>
<th>$tFS</th>
<th>$tOpts</th>
<th>$tLabel</th>
<th>$tAct</th>
</tr>"

  IFS="
"
  str=$( cat $mos/mounts/mounts.conf )
  for i in $str ; do
    cpo=$( echo "$i" | cut -d"	" -f 1 )
    cde=$( echo "$i" | cut -d"	" -f 2 )
    cfs=$( echo "$i" | cut -d"	" -f 3 )
    cop=$( echo "$i" | cut -d"	" -f 4 )
    cla=$( echo "$i" | cut -d"	" -f 5 )

    if [ -d $cpo ] ; then
      if [ ! -z "$( ls $cpo )" ] ; then
        ss="<a class=\"stop\" href=\"?page=mounts&action=umount&point=$cpo\">$tBtUmount</a>"
        echo "<tr class=\"c_start\">"
      else
        ss="<a class=\"start\" href=\"?page=mounts&action=mount&point=$cpo\">$tBtMount</a>"
        echo "<tr class=\"c_list\">"
      fi
    else
      ss="<a class=\"start\" href=\"?page=mounts&action=mount&point=$cpo\">$tBtMount</a>"
      echo "<tr class=\"c_stop\">"
    fi
    echo "<td>$cpo</td>"
    echo "<td>$cde</td>"
    echo "<td>$cfs</td>"
    echo "<td>$cop</td>"
    echo "<td>$cla</td>"

    echo "<td><div class=\"buttons\">
$ss
<a class=\"ena\" href=\"?page=mounts&action=edit&point=$cpo\">$tBtEdit</a>
<a class=\"stop\" href=\"?page=mounts&action=delete&point=$cpo\">$tBtDelete</a>
</div></td>"

    echo "</tr>"
  done
  echo "</table><br />"

fi

echo "<table border=0>
<caption><h4>$tEditTitle</h4></caption>
<form action=\"?page=mounts\" method=\"post\" enctype=\"text/plain\">
<tr><td>$tMountPoint</td><td><input name=point type=text value=\"$cPoint\" size=40></td></tr>
<tr><td>$tMountDev</td><td><input name=dev type=text value=\"$cDevice\" size=40></td></tr>
<tr><td>$tMountFS</td><td><select name=fs size=1>"
for i in $( cat /proc/filesystems | sed 's/nodev//;s/	//' ) ; do
	if [ "$i" = "$cFS" ]; then
		sel=" selected"
	else
		sel=
	fi
	echo "<option value=\"$i\"$sel>$i</option>"
done
echo "</select></td></tr>
<tr><td>$tMountOpts</td><td><input name=option type=text value=\"$cOption\" size=40>&nbsp;</td></tr>
<tr><td>$tMountLabel</td><td><input name=label type=text value=\"$cLabel\" size=40>&nbsp;</td></tr>
<tr><td /><td><div class=\"buttons\">
<button class=\"save\" type=\"submit\">$tMountSave</button>
</div></td></tr></form></table>"
